import 'package:flutter/material.dart';
import 'package:flutter_demo/page/main/main_page.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_public/config/public_language.dart';
import 'package:flutter_public/flutter_public.dart';

import 'generated/l10n.dart';

final GlobalKey<MyAppState> mainKey = GlobalKey<MyAppState>();

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

void main() {
  FlutterPublic.init(navigatorKey: navigatorKey);
  runApp(MyApp(key: mainKey));
}

class MyApp extends StatefulWidget{
  const MyApp({super.key});

  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {

  late Locale locale;

  @override
  void initState() {
    super.initState();
    locale = Locale.fromSubtags(languageCode: PublicLanguage.languageLocalCode);
  }

  void updateLanguage(Language language) {
    PublicLanguage.language = language;
    setState(() {
      locale = Locale.fromSubtags(languageCode: PublicLanguage.languageLocalCode);
    });
  }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // TRY THIS: Try running your application with "flutter run". You'll see
        // the application has a blue toolbar. Then, without quitting the app,
        // try changing the seedColor in the colorScheme below to Colors.green
        // and then invoke "hot reload" (save your changes or press the "hot
        // reload" button in a Flutter-supported IDE, or press "r" if you used
        // the command line to start the app).
        //
        // Notice that the counter didn't reset back to zero; the application
        // state is not lost during the reload. To reset the state, use hot
        // restart instead.
        //
        // This works for code too, not just values: Most code changes can be
        // tested with just a hot reload.
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MainPage(),
      // 初始化Loading
      builder: EasyLoading.init(),
      // 配置多语言
      locale: locale,
      localizationsDelegates: const [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: S.delegate.supportedLocales,
    );
  }
}
